package com.bill.gaty.mfidelity.webservices;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.util.Log;
 
import com.bill.gaty.mfidelity.beans.Compatibilite;
import com.bill.gaty.mfidelity.beans.Gtf;
import com.bill.gaty.mfidelity.dto.HistoriqueGtfRestaurationResponse;
import com.bill.gaty.mfidelity.dto.SauvegardeRestaurationResponse;
import com.bill.gaty.mfidelity.util.GtfConstantes;
import com.google.gson.Gson;

public class WebService {
	
 
public static HashMap<Integer, SauvegardeRestaurationResponse> storeGtf(String key,String password,List<Gtf> gtfs){
	return null;
	
}
public static SauvegardeRestaurationResponse storeGtf(String key,String password,Gtf gtf){
	Gson g=new  Gson();
	String data=g.toJson(gtf, Gtf.class);
	 DefaultHttpClient client=new DefaultHttpClient();
	  HttpPost post=new HttpPost(GtfConstantes.URL_SAUVEGARDE_CLOUD_GTF);
	  NameValuePair values= new BasicNameValuePair(GtfConstantes.SAUVEGARDE_PASSWORD_FIELD, password);
	  List<NameValuePair> parameters=new ArrayList<NameValuePair>();
	  parameters.add(values);
	  values= new BasicNameValuePair(GtfConstantes.SAUVEGARDE_USERNAME_FIELD, key);
	  parameters.add(values);
	  values= new BasicNameValuePair(GtfConstantes.SAUVEGARDE_CLOUD_TYPE_NOM_CLEF, GtfConstantes.SAUVEGARDE_CLOUD_TYPE_SAUVER);
	  	  parameters.add(values);
	  	values= new BasicNameValuePair(GtfConstantes.SAUVEGARDE_DONNEES_FIELD, data);
	  	  parameters.add(values);
	  	values= new BasicNameValuePair(GtfConstantes.SAUVEGARDE_LOCALE_FIELD, Locale.getDefault().getLanguage());
	  	  parameters.add(values);
	  	values= new BasicNameValuePair(GtfConstantes.SAUVEGARDE_TON_NOM_FIELD, gtf.getVotreNom());
	  	  parameters.add(values);
	  	values= new BasicNameValuePair(GtfConstantes.SAUVEGARDE_LE_NOM_DE_VOTRE_AMI_FIELD, gtf.getNomDeVotreAmi());
	  	  parameters.add(values);
	  	  
	  	 try {
				post.setEntity(new UrlEncodedFormEntity(parameters));
				String JsonResponse=WebService.computeRequest(client, post);
				  SauvegardeRestaurationResponse reponse=g.fromJson(JsonResponse, SauvegardeRestaurationResponse.class);
				 return reponse; 
	  	 }catch (Exception e) {
			// TODO: handle exception
	  		 e.printStackTrace();
		}
	return null;
	
}
public static HistoriqueGtfRestaurationResponse  retriveGtf(String key,String password,int offset,int maxresult){
	 
	 DefaultHttpClient client=new DefaultHttpClient();
	 Log.i("GtfConstantes.URL_SAUVEGARDE_CLOUD_HISTORIQUE",GtfConstantes.URL_SAUVEGARDE_CLOUD_HISTORIQUE);
	 Log.i("retriveGtf.key",key);
	 Log.i("retriveGtf.password",password);
	 Log.i("retriveGtf.offset",offset+"");
	 Log.i("retriveGtf.maxresult",maxresult+"");
				 
	  HttpPost post=new HttpPost(GtfConstantes.URL_SAUVEGARDE_CLOUD_HISTORIQUE);
	  NameValuePair values= new BasicNameValuePair(GtfConstantes.SAUVEGARDE_PASSWORD_FIELD, password);
	  List<NameValuePair> parameters=new ArrayList<NameValuePair>();
	  parameters.add(values);
	  values= new BasicNameValuePair(GtfConstantes.SAUVEGARDE_USERNAME_FIELD, key);
	  parameters.add(values);
	  values= new BasicNameValuePair(GtfConstantes.RESTAURATION_CLOUD_TYPE_RESTAURER_HISTORIQUE_GTF_CLEF, GtfConstantes.RESTAURATION_CLOUD_TYPE_HISTORIQUMFIDELITY);
	  	  parameters.add(values);
	  	  
	  	values= new BasicNameValuePair(GtfConstantes.SAUVEGARDE_MAXRESULT_FIELD, maxresult+"");
	  	  parameters.add(values);
	  	values= new BasicNameValuePair(GtfConstantes.SAUVEGARDE_LOCALE_FIELD, Locale.getDefault().getLanguage());
	  	  parameters.add(values);
	  	values= new BasicNameValuePair(GtfConstantes.SAUVEGARDE_OFFSET_FIELD, ""+offset);
	  	  parameters.add(values);
	  	   
	  	 try {
				post.setEntity(new UrlEncodedFormEntity(parameters));
				String JsonResponse=WebService.computeRequest(client, post);
				Log.i("HistoriqueGtfRestaurationResponse.JsonResponse",JsonResponse+"");
				Gson g=new Gson();
				HistoriqueGtfRestaurationResponse reponse=g.fromJson(JsonResponse, HistoriqueGtfRestaurationResponse.class);
				Log.i("HistoriqueGtfRestaurationResponse.reponse",reponse+"");
				return reponse; 
	  	 }catch (Exception e) {
			// TODO: handle exception
	  		 e.printStackTrace();
		}
	return null;
	
}
public boolean storeCompatibilite(String key,String password,List<Compatibilite> compatibilites){
	return false;
	
}
public List<Compatibilite>  retriveCompatibilite(String key,String password){
	return null;
	
}
public  Compatibilite   searchCompatibilite(  String lui,  String elle){
	 
			DefaultHttpClient client=new DefaultHttpClient();
			  HttpPost post=new HttpPost(GtfConstantes.COMPABILITY_SEARCH_URL);
			  NameValuePair values= new BasicNameValuePair(GtfConstantes.COMPABILITY_SAERCH_FILD_1_LUI_NAME, lui);
			  List<NameValuePair> parameters=new ArrayList<NameValuePair>();
			  parameters.add(values);
			  values= new BasicNameValuePair(GtfConstantes.COMPABILITY_SAERCH_FILD_2_ELLE_NAME, elle);
			  parameters.add(values);
			   try {
				post.setEntity(new UrlEncodedFormEntity(parameters));
				 Log.i("searchCompatibilite-lui",lui+"");
				  Log.i("searchCompatibilite-elle",elle+"");
				  Log.i("searchCompatibilite-url",post.getURI().toString()+"");
				  Log.i("searchCompatibilite-method",post.getMethod()+"");
				  Log.i("searchCompatibilite-setEntity",post.getEntity().toString()+"");
					
				HttpResponse response = client.execute(post);
				Log.i("searchCompatibilite-satus-code",response.getStatusLine().getStatusCode()+"");
				 
				String ligne=null;
			 
				ligne=WebService.computeRequest(client,post);
				Log.i("searchCompatibilite-final",ligne+"");
			Document doc= Jsoup.parse(ligne);
			Element div = doc.getElementById(GtfConstantes.COMPABILITY_SAERCH_DIV_ID);
			
			Elements h1s =div.getElementsByTag("h1");
			StringBuilder sb=new StringBuilder();
			for (Element element : h1s) {
				sb.append(element.toString());
			}
			sb.append(div.getElementsByTag("table").first().toString());
			Compatibilite c=new Compatibilite();
			c.setSearchCode(response.getStatusLine().getStatusCode());
			c.setElle(elle);
			c.setLui(lui);
			
			c.setResult(sb.toString());//Translator.translate("fr", translateTo,  sb.toString()));
			 
			Log.i("c.getResult",c.getResult()+"");
			 return c;
						} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
							Log.i("UnsupportedEncodingException",e.getMessage()+"");
			} catch (ClientProtocolException e) {
				// TODO Auto-generated catch block
				//Toast.makeText(null, e.getMessage(), Toast.LENGTH_LONG);
				Log.i("ClientProtocolException",e.getMessage()+"");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				//Toast.makeText(null, e.getMessage(), Toast.LENGTH_LONG);
				Log.i("IOException",e.getMessage()+"");
			}
			 
		 
	 
	return null;
}
public static String computeRequest(DefaultHttpClient client,HttpUriRequest request){
	try{
		
		HttpResponse response = client.execute(request);
		Log.i("computeRequest-satus-code",response.getStatusLine().getStatusCode()+"");
		
		BufferedReader r=new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
		StringBuilder sb=new StringBuilder();
		String ligne=null;
		int i=1;
		while(null!=(ligne=r.readLine())){
			Log.i("computeRequest-"+(i++),ligne+"");
			sb.append(ligne);
		}
		return sb.toString();
	}catch (Exception e) {
		// TODO: handle exception
		e.printStackTrace();
		return null;
	}
	
	
}
}
